//
//  QRScanner.m
//  HelloCordova
//
//  Created by Aim on 2018/12/4.
//

#import "QRScanner.h"
#import <AVFoundation/AVFoundation.h>
#import "SGQRCodeObtain.h"
#import "SGQRCodeScanView.h"
#import "SGQRCodeObtainConfigure.h"
#import "WCQRCodeViewController.h"

@interface QRScanner()<ScanResultDelegate>{
    CDVInvokedUrlCommand *_command;
}

@property (nonatomic, strong) UINavigationController *scanNav;

@end

@implementation QRScanner

- (void)prepScanner:(CDVInvokedUrlCommand *)command{
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if (device) {
        AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        switch (status) {
            case AVAuthorizationStatusNotDetermined: {
                [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
                    if (granted) {
                        dispatch_sync(dispatch_get_main_queue(), ^{
                            [self beginScan:command];
                        });
                    } else {
                        CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
                                                   messageAsString:@"用户未授权"];
                        [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
                    }
                }];
                break;
            }
            case AVAuthorizationStatusAuthorized: {
                [self beginScan:command];
                break;
            }
            case AVAuthorizationStatusDenied: {
                CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
                                                            messageAsString:@"用户拒绝访问相机"];
                [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
                break;
            }
                
            case AVAuthorizationStatusRestricted: {
                CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
                                                            messageAsString:@"相机权限异常"];
                [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
                break;
            }
                
            default:
                break;
        }
    }
}

- (void)beginScan:(CDVInvokedUrlCommand *)command{
    _command = command;
    WCQRCodeViewController *scanVctl = [[WCQRCodeViewController alloc] init];
    scanVctl.delegate = self;
    _scanNav = [[UINavigationController alloc] initWithRootViewController:scanVctl];
    [self.viewController presentViewController:_scanNav animated:YES completion:^{
    }];
}

- (void)scan:(CDVInvokedUrlCommand *)command{
    [self prepScanner:command];
}

- (void)show:(CDVInvokedUrlCommand *)command{
    [self prepScanner:command];
}

- (void)cancelScan:(CDVInvokedUrlCommand *)command{
    [self destroy:command];
}

- (void)destroy:(CDVInvokedUrlCommand *)command{
    [_scanNav dismissViewControllerAnimated:YES completion:^{
        
    }];
}

#pragma mark -- ScanResultDelegate

- (void)scanDidFinishedWithResult:(NSString *)result{
    if (_command) {
        CDVPluginResult *scanResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
                                                    messageAsString:result];
        [self.commandDelegate sendPluginResult:scanResult callbackId:_command.callbackId];
        
        _command = nil;
    }
}

- (void)scanDidCanceled{
    if (_command) {
        CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
                                                    messageAsString:@"scan_canceled"];
        [self.commandDelegate sendPluginResult:result callbackId:_command.callbackId];
        
        _command = nil;
    }
}

@end
